﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Collections.Generic;
/// <summary>
/// Summary description for Tools
/// </summary>
public class Tools
{
	public Tools()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// 转半角的函数(DBC case)
    /// </summary>
    /// <param name="input">任意字符串</param>
    /// <returns>半角字符串</returns>
    ///<remarks>
    ///全角空格为12288，半角空格为32
    ///其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
    ///</remarks>
    public static string ToDBC(string input)
    {
        char[] c = input.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            if (c[i] == 12288)
            {
                c[i] = (char)32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char)(c[i] - 65248);
        }
        return new string(c);
    }

    /// <summary>
    /// 返回背景色
    /// </summary>
    /// <param name="rowindex"></param>
    /// <returns></returns>
    public static string getRowBgColor(int rowindex) {
        return ((rowindex + 1) % 2 != 0 ? "#FFFFFF" : "#f5f4f4");
    }

    /// <summary>
    /// 返回背景色
    /// </summary>
    /// <param name="rowindex"></param>
    /// <returns></returns>
    public static string getRowBgColor2(int rowindex)
    {
        return (rowindex % 2 != 0 ? "#FFFFFF" : "#f5f4f4");
    }

    /// <summary>
    /// 行换颜色
    /// </summary>
    /// <param name="overcolor"></param>
    /// <returns></returns>
    public static string getRowChangeStyle(string color)
    {
        return "onmouseout=\" this.style.backgroundColor='" + color + "';\" onmouseover=\" this.style.backgroundColor='#fff5d1';\"";
    }

    /// <summary>
    /// 返回投资类型名称
    /// </summary>
    /// <param name="investtype"></param>
    /// <returns></returns>
    public static string getInvestTypeName(int investtype) {
        string str = null;
        switch (investtype)
        { 
            case 0:
                str = "股票型";
                break;
            case 1:
                str = "债券型";
                break;
            case 2:
                str = "混合型";
                break;
            case 3:
                str = "货币型";
                break;
            default:
                str = "未知";
                break;
        
        }
        return str;
    }

    /// <summary>
    /// 返回基金类型名称
    /// </summary>
    /// <param name="fundtype"></param>
    /// <returns></returns>
    public static string getFundTypeName(int fundtype)
    {
        string str = null;
        switch (fundtype)
        {
            case 1:
                str = "契约型封闭式";
                break;
            case 2:
                str = "契约型开放式";
                break;
            case 3:
                str = "其它";
                break;
            case 4:
                str = "ETF";
                break;
            case 5:
                str = "LOFs";
                break;
            case 6:
                str = "外币基金";
                break;
            case 7:
                str = "创新型封闭式";
                break;
            case 8:
                str = "创新型开放式";
                break;
            default:
                str = "未知";
                break;

        }
        return str;
    }

    /// <summary>
    /// 格式化输出
    /// </summary>
    /// <param name="value"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public static string stringFormat(double value, int type)
    {
        string str = null;
        switch (type)
        {
            case 10000:
                str = (value / type).ToString("f2");
                break;
            case 2:
                str = value.ToString("f2");
                break;
            case 4:
                str = value.ToString("f4");
                break;
        }
        return str;
    }

    public static string DateFormat(DateTime dt) {
        string str = null;
        if (dt != Convert.ToDateTime("0001-1-1 0:00:00"))
        {
            return dt.ToString("yyyy-MM-dd");
        }
        return str;

    }

    public static string DateFormat(DateTime? dt)
    {
        string str = "";
        if (dt != null)
        {
            return dt.Value.ToString("yyyy-MM-dd");
        }
        return str;

    }

    /// <summary>
    /// 数字变换颜色
    /// </summary>
    /// <param name="val"></param>
    /// <param name="tag"></param>
    /// <param name="formatStr"></param>
    /// <returns></returns>   
    public static string getUpdownStyle(double value, string formatStr)
    {
        if (value > 0)
            return "<span class=\"font_color_red\">" + value.ToString(formatStr) + "</span>";
        else if (value < 0)
            return "<span class=\"font_color_green\">" + value.ToString(formatStr) + "</span>";
        else
            return value.ToString(formatStr);
    }


    /// <summary>
    /// 输出评级图片
    /// </summary>
    /// <param name="score"></param>
    /// <returns></returns>
    public static string displayPJImage(double score) {
        string imgsrc = null;
        double maxvalue = Math.Floor(score);
        for (int i = 0; i < maxvalue; i++)
            imgsrc += "<img src=\"http://i.ssimg.cn/images/fund/fund08_29.gif\">";
        if(maxvalue!=score)
            imgsrc += "<img src=\"http://i.ssimg.cn/images/fund/fund08_30.gif\">";

        return imgsrc;
    }

    /// <summary>
    /// 输出评级图片
    /// </summary>
    /// <param name="score"></param>
    /// <returns></returns>
    public static string displayPJImageIndex(double score)
    {
        string imgsrc = null;
        double maxvalue = Math.Floor(score);
        for (int i = 0; i < maxvalue; i++)
            imgsrc += "<img src=\"http://i.ssimg.cn/images/fund/fund08_29.gif\">";
        if (maxvalue != score)
            imgsrc += "<img src=\"http://i.ssimg.cn/images/fund/fund08_30.gif\">";

        return imgsrc;
    }

    /// <summary>
    /// 排序字段
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string getSortFields(string str,string defaultstr) {
        string col = defaultstr;
        if ((System.Web.HttpContext.Current.Request["col"] != null) && (System.Web.HttpContext.Current.Request["col"] != ""))
        {
            col = System.Web.HttpContext.Current.Request["col"].Trim();

            if (str.IndexOf(',') != -1)
            {
                string[] sortfields = str.Split(',');
                for (int i = 0; i < sortfields.Length; i++)
                {
                    if (sortfields[i].Equals(col))
                    {
                        col = sortfields[i];
                        break;
                    }
                }
            }
            else
                if (str.Equals(col))
                    col = str;
        }        
        return col;
    }

    /// <summary>
    /// 排序顺序
    /// </summary>
    /// <returns></returns>
    public static string getOrderByTag()
    {
        string tag = "desc";
        if ((System.Web.HttpContext.Current.Request["tag"] != null) && (System.Web.HttpContext.Current.Request["tag"] != ""))
        {
            tag = System.Web.HttpContext.Current.Request["tag"].Trim();
            if (tag.Equals("asc"))
                return tag;
        }
        return tag;
    }

    /// <summary>
    /// 排序顺序
    /// </summary>
    /// <param name="tag"></param>
    /// <returns></returns>
    public static string getOrderByTag(string currentfields, string sortfields, string tag)
    {
        if (currentfields.Equals(sortfields))
        {
            if (tag.Equals("desc"))
                return "asc";
            else
                return "desc";
        }
        else
            return "desc";
    }

    /// <summary>
    /// 排序图标
    /// </summary>
    /// <param name="tag"></param>
    /// <returns></returns>
    public static string getOrderByIcon(string currentfields, string sortfields, string tag)
    {
        if (currentfields.Equals(sortfields) || (currentfields == "BDL" && sortfields == "Valuetime desc,BDL"))
        {
            if (tag.Equals("desc"))
                return "▼";
            else
                return "▲";
        }
        else
            return null;
    }

    /// <summary>
    /// 排序的查询条件
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string getSearchParameter(string str)
    {
        string searchparameter = null; 
        if (str.IndexOf(',') != -1)
        {
            string[] searchfields = str.Split(',');
            {
                for (int i = 0; i < searchfields.Length; i++)
                {
                    if ((System.Web.HttpContext.Current.Request[searchfields[i]] != null) && (System.Web.HttpContext.Current.Request[searchfields[i]] != ""))
                    {
                        searchparameter += "&" + searchfields[i] + "=" + System.Web.HttpContext.Current.Request[searchfields[i]].Trim();
                    }
                }
            }
        }else
            if ((System.Web.HttpContext.Current.Request[str] != null) && (System.Web.HttpContext.Current.Request[str] != ""))
                searchparameter += "&" + str + "=" + System.Web.HttpContext.Current.Request[str].Trim();

        return searchparameter;
    }

    /// <summary>
    /// 查询的页面来源
    /// </summary>
    /// <returns></returns>
    public static string getSearchUrl() {
        return System.Web.HttpContext.Current.Request.ServerVariables["Url"];
    }

    /// <summary>
    /// 返回颜色Css值。大于0，红色，小于0，绿色。等于0 黑色
    /// </summary>
    /// <param name="value"></param>
    /// <param name="basevalue"></param>
    /// <returns></returns>
    public static string getColorCssValue(double value,double basevalue)
    {
        if (value > basevalue)
        {
            return "red_color";
        }
        else if (value == basevalue)
        {
            return "gray_color";
        }
        else
        {
            return "green_color";
        }
    }

    /// <summary>
    /// json 输出
    /// </summary>
    /// <param name="json"></param>
    /// <param name="fn"></param>
    /// <param name="var"></param>
    /// <returns></returns>
    public static string JsonOut(string json, string fn, string var)
    {
        if (!string.IsNullOrEmpty(fn))
        {
            return fn + "(" + json + ");";
        }
        if (!string.IsNullOrEmpty(var))
        {
            return "var " + var + "=" + json + ";";
        }
        return json;
    }

    /// <summary>
    /// 获取学历转换
    /// </summary>
    /// <param name="edu"></param>
    /// <returns></returns>
    public static string getEducationString(int edu)
    { 
        switch(edu)
        {
            case 0: return "不详";
            case 1: return "博士后";
            case 2: return "博士";
            case 3: return "硕士";
            case 4: return "本科";
            case 5: return "大专";
            case 6: return "高中";
            case 7: return "中专";
            case 8: return "初中";
            case 9: return "小学";
            default: return "不详";
        }
    }

    public static string getGenderString(int gen)
    {
        switch (gen)
        {
            case 0: return "不详";
            case 1: return "男";
            case 2: return "女";
            default: return "不详";
        }
    }

    public static string getRZStatus(int status)
    {
        switch (status)
        {
            case 1: return "在职";
            case 2: return "离职";
            case 3: return "专职";
            case 4: return "兼职";
            default: return "在职";
        }
    }

    /// <summary>
    /// 将20120625字符串格式转换为2012-06-25日期格式
    /// </summary>
    /// <param name="dateString"></param>
    /// <returns></returns>
    public static DateTime convertStringToDateTime(string dateString)
    {
        int year = int.Parse(dateString.Substring(0, 4));
        int month = int.Parse(dateString.Substring(4, 2));
        int day = int.Parse(dateString.Substring(6, 2));
        return new DateTime(year, month, day);
    }

    /// <summary>
    /// 仅用于V_JRJ_FND_GEN_INFO运河接口的市场类型转换
    /// </summary>
    /// <param name="market"></param>
    public static int TradeMarketConvert(int market)
    {
        switch (market)
        { 
            case 1:
                return 2;
            case 2:
                return 1;
            default: 
                return 0;
        }
    }

    /// <summary>
    /// 从list列表中获取第一个元素
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    /// <returns></returns>
    public static string getFirstElement<T>(List<T> list)
    {
        if (list.Count > 0)
            return list[0].ToString();
        else return null;
    }

    /// <summary>
    /// 引进静态文件
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string include(string path)
    {
        return "<!--#include file=\"" + path + "\"-->";
    }

    /// <summary>
    /// 从配置文件中读取静态资源的CDN日期版本号
    /// </summary>
    /// <returns></returns>
    public static string getFundsCDNVersion(string type)
    {
        string version = null;
        switch (type)
        { 
            case "js":
                version = ConfigurationManager.AppSettings["JSCDNVersion"];
                break;
            case "css":
                version = ConfigurationManager.AppSettings["CSSCDNVersion"];
                break;
        }
        return version;      
    }
}
